package com.construction_Safety.information.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.construction_Safety.information.domain.CInspecAlbum;
import com.construction_Safety.information.domain.CMaintainAlbum;
import com.construction_Safety.information.service.ICInspecAlbumService;
import com.construction_Safety.information.service.ICMaintainAlbumService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.construction_Safety.common.annotation.Log;
import com.construction_Safety.common.core.controller.BaseController;
import com.construction_Safety.common.core.domain.AjaxResult;
import com.construction_Safety.common.enums.BusinessType;
import com.construction_Safety.information.domain.CInformation;
import com.construction_Safety.information.service.ICInformationService;
import com.construction_Safety.common.utils.poi.ExcelUtil;
import com.construction_Safety.common.core.page.TableDataInfo;


/**
 * 维修信息管理Controller
 * 
 * @author yat
 * @date 2025-06-11
 */
@RestController
@RequestMapping("/information/information")
public class CInformationController extends BaseController
{
    @Autowired
    private ICInformationService cInformationService;
    @Autowired
    private ICInspecAlbumService cInspecAlbumService;
    @Autowired
    private ICMaintainAlbumService cMaintainAlbumService;
    /**
     * 查询维修信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('information:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(CInformation cInformation)
    {
        startPage();
        List<CInformation> list = cInformationService.selectCInformationList(cInformation);
        return getDataTable(list);
    }

    /**
     * 导出维修信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('information:information:export')")
    @Log(title = "维修信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CInformation cInformation)
    {
        List<CInformation> list = cInformationService.selectCInformationList(cInformation);
        ExcelUtil<CInformation> util = new ExcelUtil<CInformation>(CInformation.class);
        util.exportExcel(response, list, "维修信息管理数据");
    }

    /**
     * 获取维修信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('information:information:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CInformation cInformation = cInformationService.selectCInformationById(id);
        cInformation.setcInspecAlbumList(cInspecAlbumService.selectCInspecAlbumList(id));
        cInformation.setcMaintainAlbumList(cMaintainAlbumService.selectCMaintainAlbumList(id));
        return success(cInformation);
    }

    /**
     * 新增维修信息管理
     */
    @PreAuthorize("@ss.hasPermi('information:information:add')")
    @Log(title = "维修信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CInformation cInformation)
    {
        List<CInspecAlbum> cInspecAlbumList=cInformation.getcInspecAlbumList();
        List<CInformation> cInformations=cInformationService.selectCInformationList(null);
        Long maxId = cInformations.stream()
                .map(CInformation::getId)
                .max(Long::compareTo)
                .orElse(null);
        if(cInspecAlbumList!=null){
            for(CInspecAlbum cInspecAlbum:cInspecAlbumList){
                cInspecAlbum.setInformationId(maxId.intValue()+1);
                cInspecAlbumService.insertCInspecAlbum(cInspecAlbum);
            }
        }
        return toAjax(cInformationService.insertCInformation(cInformation));
    }

    /**
     * 修改维修信息管理
     */
    @PreAuthorize("@ss.hasPermi('information:information:edit')")
    @Log(title = "维修信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CInformation cInformation)
    {
        List<CInspecAlbum> cInspecAlbumList=cInformation.getcInspecAlbumList();
        List<CMaintainAlbum> cMaintainAlbumList=cInformation.getcMaintainAlbumList();
        if(cInspecAlbumList!=null){
            if(cInspecAlbumService.deleteCInformationByInfoId(cInformation.getId())>=0){
                for(CInspecAlbum cInspecAlbum:cInspecAlbumList){
                    cInspecAlbum.setInformationId(cInformation.getId().intValue());
                    cInspecAlbumService.insertCInspecAlbum(cInspecAlbum);
                }
            }
        }
        if(cMaintainAlbumList!=null){
            if(cMaintainAlbumService.deleteCMaintainAlbumByInfoId(cInformation.getId())>=0){
                for(CMaintainAlbum cMaintainAlbum:cMaintainAlbumList){
                    cMaintainAlbum.setInformationId(cInformation.getId().intValue());
                    cMaintainAlbumService.insertCMaintainAlbum(cMaintainAlbum);
                }
            }
        }
        return toAjax(cInformationService.updateCInformation(cInformation));
    }

    /**
     * 删除维修信息管理
     */
    @PreAuthorize("@ss.hasPermi('information:information:remove')")
    @Log(title = "维修信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id:ids){
            cInspecAlbumService.deleteCInformationByInfoId(id);
            cMaintainAlbumService.deleteCMaintainAlbumByInfoId(id);
        }
        return toAjax(cInformationService.deleteCInformationByIds(ids));
    }
}
